package br.com.mapagay.servlets;

import br.com.mapagay.entidades.mapa.Lugar;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author Lauro
 */
public class ListaLocais extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Content-Type", "application/json");
        PrintWriter out = response.getWriter();
        try {
            String tipo = request.getParameter("tipo");
            double latitude = Double.parseDouble(request.getParameter("lat"));
            double longitude = Double.parseDouble(request.getParameter("lng"));
            String[] categoriasS = request.getParameterValues("cat");
            int[] categorias = null;
            if (categoriasS != null && !categoriasS[0].trim().isEmpty()) {
                categorias = new int[categoriasS.length];
                for (int i = 0; i < categoriasS.length; i++) {
                    categorias[i] = Integer.parseInt(categoriasS[i]);
                }
            }
            int max = request.getParameter("max") != null ? Integer.parseInt(request.getParameter("max")) : 5;

            List<Lugar> listaLugares = Lugar.buscarLugaresMaisProximos(latitude, longitude, categorias, 10.0, max);
            if (listaLugares != null) {
                JSONArray fornecedoresJSON = new JSONArray();
                for (Lugar lugar : listaLugares) {
                    JSONObject fornecedorJSON = new JSONObject();
                    fornecedorJSON.put("nome", lugar.getNome());
                    fornecedorJSON.put("imagem", lugar.getThumb());
                    fornecedorJSON.put("categoria", lugar.getCategoria().getNome());
                    fornecedorJSON.put("latitude", lugar.getLatitude());
                    fornecedorJSON.put("longitude", lugar.getLongitude());
                    fornecedorJSON.put("outdoor", ((lugar.getOutdoor() != null) ? lugar.getOutdoor().getImagem() : null));
                    fornecedoresJSON.add(fornecedorJSON);
                }
                JSONObject result = new JSONObject();
                result.put("lugares", fornecedoresJSON);
                out.println(result.toString());
            } else {
                JSONObject json = new JSONObject();
                out.println(json.toString());
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
